package skdao

import (
	"Cgo/front/models"
	"Cgo/global"
	"errors"
)

type loginDao struct{}

var LoginDao = loginDao{}

// 登录
func (l loginDao) UserLogin(name string, password string) (models.SkUser, error) {
	var userInfo models.SkUser
	if r := global.DB.Raw("SELECT * FROM sk_user WHERE name = ? AND password = ?", name, password).Scan(&userInfo); r.RowsAffected > 0 {
		return userInfo, nil
	} else {
		return userInfo, errors.New("没有查到数据")
	}
}

// 注册用户
func (l loginDao) UserRegister(id int, name string, password string) error {
	// 执行数据库插入操作
	if result := global.DB.Exec("INSERT INTO sk_user (id, name, password) VALUES (?, ?, ?)", id, name, password); result.Error != nil {
		return result.Error
	} else {
		return nil
	}
}
